package com.mybatis.demo;

import com.mybatis.demo.bean.Goods;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.Reader;

/**
 * @author xiangminwen
 * @since 2021/8/19 15:42
 */
public class MyBatisTest {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void prepare() throws Exception {
        Reader reader = Resources.getResourceAsReader("MybatisConfig.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    }

    @Test
    public void testMyBatis() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods = mapper.selectGoods(1L);
        System.out.println(goods);
        sqlSession.commit();
        sqlSession.close();
    }
}
